from django.http.response import HttpResponse
from .models import Book


def index(request):
    # 1.添加数据
    # book = Book(name='三国演义', author='罗贯中', price=100)
    # book = Book(name='水浒传', author='施耐庵', price=100)
    # book = Book(name='红楼梦', author='曹雪芹', price=80)
    # book = Book(name='西游记', author='吴承恩', price=120)
    # book.save()

    # 2.查找数据
    # 根据主键进行查找
    # book = Book.objects.get(pk=1)
    # print(book)

    # 根据其他字段进行查找
    # books = Book.objects.filter(name='水浒传')
    # print(books)
    # book = books.first()
    # print(book)

    # 查询全部数据
    books = Book.objects.all()
    for book in books:
        print(book)

    # 3.删除数据
    # book = Book.objects.get(pk=4)
    # book.delete()

    # 4.修改数据
    # book = Book.objects.get(pk=2)
    # book.price += 10
    # book.save()
    return HttpResponse('数据添加成功')
